<?php
#[更新HTML类]
#[PHPOK2.0Beta版更新HTML功能采用点击更新方式]
#[在后台仅是设置一个标识是否更新HTML]
CLASS Rewrite
{
	
	var $content;
	var $url;#[网站网址]
	var $tplfolder;#[模板目录，主要是用于更新图片地址]
	var $upfiles;#[上传文件的目录]
	var $urlarray;#[要更新的网址的数组]
	var $imgarray;#[要更新的图片的数组]
	var $actionarray;#[要更新的ACTION的数组]
	var $lang;#[模板语言包]
	var $input_array;#[更新input里的图片]
	var $js_array;#[更新里面的的JS文件路径]
	var $form_array;#[修正form里的action动作]

	function __construct($siteurl="./",$lang="zh",$tplfolder="templates",$upfiles="upfiles")
	{
		$this->url = defined("RW_SITE_URL") ? RW_SITE_URL : $siteurl;
		
		if($this->url == "./")
		{
			$this->get_siteurl();#[自动获取系统网址]
			
		}
		if(substr($this->url,-1) != "/")
		{
			$this->url .= "/";
		}
		$this->siteurl = $this->url;
		
		$this->tplfolder = defined("RW_TPL_FOLDER") ? RW_TPL_FOLDER : $tplfolder;
		$this->upfiles = defined("RW_UPFILES") ? RW_UPFILES : $upfiles;
		$this->lang = defined("RW_LANG") ? RW_LANG : $lang;
	}

	#[兼容PHP4]
	function Rewrite($siteurl="./",$lang="zh",$tplfolder="templates",$upfiles="upfiles")
	{
		$this->__construct($siteurl,$lang,$tplfolder,$upfiles);
	}

	function set_content($content)
	{
		$this->content = $content;
	}

	#[一个函数进行下面全部操作]
	function qg_rewrite($content="")
	{
		if($content)
		{
			$this->content = $content;
		}
		$this->getsrc_array();
		$this->geturl_array();
		$this->getaction_array();
		$this->replace_url();
		$this->ad_content(); //屏蔽关键词
		return $this->content;#[返回rewrite的内容]
	}
	
	#[屏蔽关键词]
	function ad_content()
	{
		require_once("class/min.class.php");
		if($min)
		{
			$badword1 = array_combine($min,array_fill(0,count($min),''));
			$this->content = strtr($this->content, $badword1);
		}
	}
	
	function geturl_array()
	{
		$this->urlarray = array();
		#[获取网址中的Url]
		preg_match_all("/href=[\"|'|]{0,}(.*?)[\"|'|]{1,}/is",$this->content,$this->urlarray);
		
		$this->urlarray = array_unique($this->urlarray[1]);
	}
	function getaction_array()
	{
		$this->actionarray = array();
		#[获取网址中的Url]
		preg_match_all("/action=[\"|'| ]{0,}(.*?)[\"|'| ]{1,}/is",$this->content,$this->actionarray);
		
		$this->actionarray = array_unique($this->actionarray[1]);
	}
	function getsrc_array()
	{
		$this->imgarray = array();
		#[获取网址中的Url]
		preg_match_all("/\<img.*?src\=\"(.*?)\"[^>]*>/i",$this->content,$this->imgarray);
		
		$this->imgarray = array_unique($this->imgarray[1]);
	}

	function replace_array($var)
	{
		if(!$var || $var == "")
		{
			return false;
		}
		$myarray = $this->$var;
		$chkcount = count($myarray);
		if($chkcount < 1)
		{
			return false;
		}
		foreach($myarray AS $key=>$value)
		{
			$this->global_replace($value);
		}
	}

	function replace_url()
	{
		$chkcount = count($this->urlarray);
		if($chkcount < 1)
		{
			return false;
		}
		#[开始网址替换]
		
		
		foreach($this->actionarray AS $key=>$value)
		{
			$value = trim($value);
			$old_value = $value;
			$value = $this->to_rewrite_url($value);
			if($value != $old_value)
			{
				$this->str_replace_array($old_value,$value);
			}
		}
		
		foreach($this->imgarray AS $key=>$value)
		{
			$value = trim($value);
			$old_value = $value;
			$value = $this->to_rewrite_url($value);
		
			if($value != $old_value)
			{
				$this->str_replace_array($old_value,$value);
			}
		}
		
		foreach($this->urlarray AS $key=>$value)
		{
			
			
			$value = trim($value);
			$old_value = $value;
			if(!$value || $value == "")
			{
				continue;
			}
			$chk_7 = substr(strtolower($value),0,7);
			#[判断是否有带http://]
			if($chk_7 == "http://" && substr(strtolower($value),0,strlen($this->siteurl)) != $this->siteurl)
			{
				continue;
			}
			#[判断是否是mailto:]
			if($chk_7 == "mailto:")
			{
				continue;
			}
			if(substr($value,0,2) == "./")
			{
				$value = substr($value,2);
			}
			#[to-rewrite-html]
			$value = $this->to_rewrite_url($value);
			#[替换，为了防止带分页的替换错误，这里进行三次替换]
			if($value != $old_value)
			{
				$this->str_replace_array($old_value,$value);
			}
		}
	}


	function global_replace($msg)
	{
		$msg = trim($msg);
		$old = $msg;
		if(!$msg || $msg == "")
		{
			return false;
		}
		if(substr(strtolower($msg),0,7) == "http://")
		{
			return false;
		}
		if(substr($msg,0,2) == "./")
		{
			$msg = substr($msg,2);
		}
		$msg = $this->siteurl.$msg;
		if($msg != $old)
		{
			$this->str_replace_array($old,$msg);
		}
		return true;
	}

	function str_replace_array($old,$new)
	{
		$this->content = str_replace("='".$old."'","='".$new."'",$this->content);
		$this->content = str_replace('="'.$old.'"','="'.$new.'"',$this->content);
		$this->content = str_replace("=".$old." ","=".$new." ",$this->content);
	}

	function to_rewrite_url($url)
	{
		#[判断参数类型]		
		if(preg_match("/[\x7f-\xff]/",$url))
		{
			$url = str_replace(array(" ","　","\t","\n","\r","/","、"),array('-','-','-','-','-','-','-'),$url);
			//$url = iconv("utf-8","gb2312",str_replace(array(" ","  ","　","/","、"),'-',trim($url)));
		}
		$chkurl = basename($url);

		//echo $chkurl."<br />";
		@$ext = strstr($chkurl,"?");
		if($ext)
		{
			@$chkurl = str_replace($ext,"",$chkurl);
		}
		
		/*首页*/
		$indexarray = array("index.php","home.php");
		/*列表页面数组*/
		$listarray = array("products.php","newproducts.php","project.php","projects.php","news.php","case.php","cases.php","team.php","factory.php");
		/*详细页面数组*/
		$detailarray = array("detail.php","msg.php","display.php","dfile.php");
		/*单页面(公司简介、联系我们)数组*/
		$specialarray = array("about.php","aboutus.php","service.php","joinus.php","join.php","contact.php","contacts.php","jobs.php","job.php","contactus.php","special.php","sitemap.php");
		/*留言页面数组*/
		$messagearray = array("book.php","feedback.php","message.php");
		
		#[首页]
		if(in_array($chkurl,$indexarray))
		{
			$ext_filename = str_replace(".php","",$chkurl);
			if($ext)
			{
				$ext_array = $this->url_ext_array($ext);
				if($ext_array["template"])
				{
					$return_url = SITE_URI.$ext_filename.".php?template=".$ext_array["template"];
				}
			}
			else
			{
				$return_url = SITE_URI.$ext_filename.".html";
			}
			return $return_url;
		}
		#[列表页]
		elseif(in_array($chkurl,$listarray))
		{
			$ext_filename = str_replace(".php","",$chkurl);
			if(!$ext)
			{
				$return_url = SITE_URI.$ext_filename.".html";
				return $return_url;
			}
			else
			{
			$ext_array = $this->url_ext_array($ext);
			$return_url = SITE_URI.$ext_filename;
			if($ext_array["cname"])
			{
				
				$ext_array["cname"] = str_replace(array(" ","　","\t","\n","\r","/","、","<",">","(",")","."),array('-','-','-','-','-','-','-','-','-','-','-','-'),strtolower($ext_array["cname"]));
				if(preg_match("/[\x7f-\xff]/",$ext_array["cname"])) 
				{
					$return_url .= "/". Pinyin($ext_array["cname"],1);
				}
				else
				{
					$return_url .= "/".$ext_array["cname"];
				}
			}
			if($ext_array["sname"])
			{
				
				$ext_array["sname"] = str_replace(array(" ","　","\t","\n","\r","/","、","<",">","(",")","."),array('-','-','-','-','-','-','-','-','-','-','-','-'),strtolower($ext_array["sname"]));
				if(preg_match("/[\x7f-\xff]/",$ext_array["sname"])) 
				{
					$return_url .= "/". Pinyin($ext_array["sname"],1);
				}
				else
				{
					$return_url .= "/".$ext_array["sname"];
				}
			}
			$return_url .= "/".$ext_array["id"];
			$ext_array["pageid"] = intval($ext_array["pageid"]);
			if($ext_array["pageid"])
			{
			$ext_array["pageid"] = $ext_array["pageid"] >0 ? $ext_array["pageid"] : 1;
			
			#[如果有模板]
			$return_url .= "/".$ext_array["pageid"];
			}
			$return_url .= ".html";
			return $return_url;
			}
		}
		#[内容页]
		elseif(in_array($chkurl,$detailarray))
		{
			
			$ext_filename = str_replace(".php","",$chkurl);
			$ext_array = $this->url_ext_array($ext);
			$return_url = SITE_URI.$ext_filename;

			if($ext_array["cname"])
			{
				
				$ext_array["cname"] = str_replace(array(" ","　","\t","\n","\r","/","、","<",">","(",")","."),array('-','-','-','-','-','-','-','-','-','-','-','-'),strtolower($ext_array["cname"]));
				if(preg_match("/[\x7f-\xff]/",$ext_array["cname"])) 
				{
					$return_url .= "/". Pinyin($ext_array["cname"],1);
				}
				else
				{
					$return_url .= "/".$ext_array["cname"];
				}
			}
			
			if($ext_array["sname"])
			{
				
				$ext_array["sname"] = str_replace(array(" ","　","\t","\n","\r","/","、","<",">","(",")","."),array('-','-','-','-','-','-','-','-','-','-','-','-'),strtolower($ext_array["sname"]));
				if(preg_match("/[\x7f-\xff]/",$ext_array["sname"])) 
				{
					$return_url .= "/". Pinyin($ext_array["sname"],1);
				}
				else
				{
					$return_url .= "/".$ext_array["sname"];
				}
			}
			$return_url .= "/".$ext_array["id"];
			if($ext_array["cid"])
			{
				$return_url .= "/".intval($ext_array["cid"]);
			}
			$return_url .= ".html";
			
			if(is_numeric($ext_array["id"]) || is_numeric($ext_array["cid"]))
			{
				if(preg_replace('|[0-9]+|','',$ext_array["id"]))
				{
					$reutnm = preg_replace('|[0-9]+|','',$ext_array["id"]);
				}
				if(preg_replace('|[0-9]+|','',$ext_array["cid"]))
				{
					$reutnm = preg_replace('|[0-9]+|','',$ext_array["cid"]);
				}
				$return_url .= $reutnm;
			}
			
			return $return_url;
		}
		#[单页面]
		elseif(in_array($chkurl,$specialarray))
		{
			$ext_filename = str_replace(".php","",$chkurl);
			if(!$ext)
			{
				$return_url = SITE_URI.$ext_filename;
			}
			else
			{
				$ext_array = $this->url_ext_array($ext);
				$return_url = SITE_URI.$ext_filename;
				if($ext_array["id"])
				{
					$return_url .= "/".$ext_array["id"];
				}
				$ext_array["pageid"] = intval($ext_array["pageid"]);
				if($ext_array["pageid"] && $ext_array["pageid"] != 0)
				{
					$return_url .= "/".$ext_array["pageid"];
				}
			}
			
			$return_url .= ".html";
			return $return_url;
		}
		elseif(strstr($chkurl,"register.php"))
		{
			$ext_array = $this->url_ext_array($ext);
			$return_url = SITE_URI."register";
			if($ext_array["act"] == "regok")
			{
				$return_url .= "/regok";
			}
			$return_url .= ".html";
			return $return_url;
		}
		elseif(strstr($url,"upfiles/"))
		{
			$return_url = SITE_URI.$url;
			return $return_url;
		}
		/*elseif(strstr($chkurl,"search.php"))
		{
			$ext_array = $this->url_ext_array($ext);
			
			
				$return_url = SITE_URI."search";
				if($ext_array["act"] == "searchlink")
				{
					$return_url .= "/searchlink";
				}
				if($ext_array["act"] == "searchok")
				{
					$return_url .= "/searchok";
				}
				
				$return_url .= ".html";
				return $return_url;
		}*/
		elseif(strstr($chkurl,"login.php"))
		{
			$ext_array = $this->url_ext_array($ext);
			
			
				$return_url = SITE_URI."login";
				if($ext_array["act"] == "loginok")
				{
					$return_url .= "/loginok";
				}
				if($ext_array["act"] == "logout")
				{
					$return_url .= "/logout";
				}
				$return_url .= ".html";
				return $return_url;
		}
		
		/*elseif(strstr($chkurl,"cart.php"))
		{
			$ext_array = $this->url_ext_array($ext);
			
			
				$return_url = SITE_URI."cart";
				if($ext_array["act"] == "cart")
				{
					$return_url .= "/cart";
				}
				elseif($ext_array["act"] == "buy")
				{
					$return_url .= "/buy";
				}
				$return_url .= ".html";
				return $return_url;
		}*/
		
		/*elseif(in_array($chkurl,$messagearray))
		{
		
			$ext_filename = str_replace(".php","",$chkurl);
	

				$return_url = SITE_URI.$ext_filename;

				$ext_array = $this->url_ext_array($ext);
				if($ext_array["act"] == "addok")
				{
					$return_url .= "/addok";
				}
				$return_url .= ".html";
				return $return_url;
		}*/
		else
		{
			return $url;
		}
	}

	function url_ext_array($ext)
	{
		if(substr($ext,0,1) == "?")
		{
			$ext = substr($ext,1);
		}
		$array = $explode_array = array();
		$explode_array = explode("&",$ext);
		foreach($explode_array AS $key=>$value)
		{
			$value = trim($value);
			if($value)
			{
				$v_m = explode("=",$value);
				if($v_m[0] && $v_m[1])
				{
					$array[$v_m[0]] = $v_m[1];
				}
			}
		}
		return $array;
	}


	function set_lang($lang)
	{
		$this->lang = $lang;
	}

	function set_tplfolder($tplfolder)
	{
		$this->tplfolder = $tplfolder;
	}

	function set_upfiles($upfiles)
	{
		$this->upfiles = $upfiles;
	}

	function set_siteurl($url)
	{
		$this->url = $url;
	}

	function get_siteurl()
	{
		$myurl = "http://".str_replace("http://","",$_SERVER["SERVER_NAME"]);
		$docu = $_SERVER["PHP_SELF"];
		$array = explode("/",$docu);
		$count = count($array);
		if($count>1)
		{
			foreach($array AS $key=>$value)
			{
				$value = trim($value);
				if($value)
				{
					if(($key+1) < $count)
					{
						$myurl .= "/".$value;
					}
				}
			}
		}
		$myurl .= "/";
		$this->url = $myurl;
	}
}
?>